<?php
/**
 * Created by PhpStorm.
 * User: zhangrxiang
 * Date: 2017/7/10
 * Time: 21:59
 */

function unix_to_greg( $unix_timestamp ) {
	$julian = floor( ( ( ( $unix_timestamp / 60 ) / 60 ) / 24 ) + 2440588 );
	$julian = $julian - 1721119;
	$calc1  = 4 * $julian - 1;
	$year   = floor( $calc1 / 146097 );
	$julian = floor( $calc1 - 146097 * $year );
	$day    = floor( $julian / 4 );
	$calc2  = 4 * $day + 3;
	$julian = floor( $calc2 / 1461 );
	$day    = $calc2 - 1461 * $julian;
	$day    = floor( ( $day + 4 ) / 4 );
	$calc3  = 5 * $day - 3;
	$month  = floor( $calc3 / 153 );
	$day    = $calc3 - 153 * $month;
	$day    = floor( ( $day + 5 ) / 5 );
	$year   = 100 * $year + $julian;

	if ( $month < 10 ) {
		$month = $month + 3;
	} else {
		$month = $month - 9;
		$year  = $year + 1;
	}

	return "$day.$month.$year";
}


echo unix_to_greg( time() );

function jd_to_greg( $julian ) {
	$julian = $julian - 1721119;
	$calc1  = 4 * $julian - 1;
	$year   = floor( $calc1 / 146097 );
	$julian = floor( $calc1 - 146097 * $year );
	$day    = floor( $julian / 4 );
	$calc2  = 4 * $day + 3;
	$julian = floor( $calc2 / 1461 );
	$day    = $calc2 - 1461 * $julian;
	$day    = floor( ( $day + 4 ) / 4 );
	$calc3  = 5 * $day - 3;
	$month  = floor( $calc3 / 153 );
	$day    = $calc3 - 153 * $month;
	$day    = floor( ( $day + 5 ) / 5 );
	$year   = 100 * $year + $julian;

	if ( $month < 10 ) {
		$month = $month + 3;
	} else {
		$month = $month - 9;
		$year  = $year + 1;
	}

	return "$day.$month.$year";
}

echo PHP_EOL;
echo $julian = floor( ( ( ( time() / 60 ) / 60 ) / 24 ) + 2440588 );
echo PHP_EOL;
echo jd_to_greg( $julian );

echo PHP_EOL . str_repeat( "*", 50 ) . PHP_EOL;
//GregorianToJD — 转变一个Gregorian历法日期到Julian Day计数
$gregoriantojd = gregoriantojd( "7", "10", "2017" );
echo $gregoriantojd;